package cn.signalpha.modules.auth.endpoint;

import cn.signalpha.common.annotation.FormRepeatCommitAnnotation;
import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.sms.model.SmsCode;
import cn.signalpha.core.sms.model.SmsData;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.DigestUtil;
import cn.signalpha.modules.auth.dto.SignUpDTO;
import cn.signalpha.modules.com.dto.UserDTO;
import cn.signalpha.modules.com.entity.User;
import cn.signalpha.modules.com.service.UserService;
import cn.signalpha.modules.resource.builder.sms.SmsBuilder;
import cn.signalpha.modules.resource.utils.SmsUtil;
import cn.signalpha.modules.shop.entity.Seller;
import cn.signalpha.modules.shop.service.SellerService;
import cn.signalpha.modules.system.service.impl.UserServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;
import java.util.Objects;

import static cn.signalpha.modules.resource.utils.SmsUtil.*;

/**
 * 类说明
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_AUTH_NAME)
@Api(value = "用户注册", tags = "用户注册")
public class RegisterEndPoint {

	private final SmsBuilder smsBuilder;
	private final UserService userService;
	private final SellerService sellerService;
	private final UserServiceImpl userSysService;

	@PostMapping("/sign-up")
	@FormRepeatCommitAnnotation
	@ApiOperation(value = "注册", notes = "")
	public R signUp(@Valid @RequestBody SignUpDTO signUp){
		//测试环境不需要验证码
//		ConfigurableEnvironment environment = new StandardEnvironment();
//		String activeProfiles = environment.getActiveProfiles()[0];
//		if(activeProfiles.equals(AppConstant.PROD_CODE)){
//		}
		Assert.isTrue(smsBuilder.template().validateMessage(new SmsCode().setPhone(signUp.getMobile()).setValue(signUp.getSmsCode())), SmsUtil.VALIDATE_FAIL);
		userService.createUser(Objects.requireNonNull(BeanUtil.copy(signUp, User.class)), signUp.getShareCode());
		return R.success("注册成功^_^");
	}

	@SneakyThrows
	@GetMapping("/sign-up/sms/code")
	@ApiOperation(value = "获取短信", notes = "")
	public R signUpSmsCode(@RequestParam String mobile){
		Map<String, String> params = SmsUtil.getValidateParams();
		SmsCode smsCode = smsBuilder.template().sendValidate(new SmsData(params).setKey(PARAM_KEY), mobile);
		return smsCode.isSuccess() ? R.data(smsCode, SEND_SUCCESS) : R.fail(SEND_FAIL);
	}

	@PostMapping("/update/loginPwd")
	@ApiOperation(value = "忘记密码 - 前端", notes = "")
	public R loginPwdUpdate(@RequestBody UserDTO.UpdatePwdDTO updatePwd){
		//测试环境不需要验证码
//		ConfigurableEnvironment environment = new StandardEnvironment();
//		String activeProfiles = environment.getActiveProfiles()[0];
//		if(activeProfiles.equals(AppConstant.PROD_CODE)){
//		}
		Assert.isTrue(smsBuilder.template().validateMessage(new SmsCode().setPhone(updatePwd.getMobile()).setValue(updatePwd.getSmsCode())), SmsUtil.VALIDATE_FAIL);
		User user = userService.getByMobile(updatePwd.getMobile());
		Assert.isNull(user, MsgConstant.ACCOUNT_NULL);
		return R.status(userService.updateById(User.builder().id(user.getId()).loginPwd(DigestUtil.encrypt(updatePwd.getPassword())).build()));
	}

	@PostMapping("/update/loginPwd/system")
	@ApiOperation(value = "忘记密码 - 总后台", notes = "")
	public R loginPwdUpdateSystem(@RequestBody UserDTO.UpdatePwdDTO updatePwd){
		Assert.isTrue(smsBuilder.template().validateMessage(new SmsCode().setPhone(updatePwd.getMobile()).setValue(updatePwd.getSmsCode())), SmsUtil.VALIDATE_FAIL);
		cn.signalpha.modules.system.entity.User user = userSysService.getOne(Wrappers.<cn.signalpha.modules.system.entity.User>lambdaQuery().eq(cn.signalpha.modules.system.entity.User::getPhone, updatePwd.getMobile()));
		Assert.isNull(user, MsgConstant.ACCOUNT_NULL);

		cn.signalpha.modules.system.entity.User userUpdate = new cn.signalpha.modules.system.entity.User();
		userUpdate.setId(user.getId());
		userUpdate.setPassword(DigestUtil.encrypt(updatePwd.getPassword()));
		userSysService.updateById(userUpdate);
		return R.status(true);
	}

	@PostMapping("/update/loginPwd/seller")
	@ApiOperation(value = "忘记密码 - 商户后台", notes = "")
	public R loginPwdUpdateSeller(@RequestBody UserDTO.UpdatePwdDTO updatePwd){
		Assert.isTrue(smsBuilder.template().validateMessage(new SmsCode().setPhone(updatePwd.getMobile()).setValue(updatePwd.getSmsCode())), SmsUtil.VALIDATE_FAIL);
		Seller seller = sellerService.getOne(Wrappers.<Seller>lambdaQuery().eq(Seller::getMobile, updatePwd.getMobile()));
		Assert.isNull(seller, MsgConstant.ACCOUNT_NULL);

		Seller sellerUpdate = new Seller();
		sellerUpdate.setId(seller.getId());
		sellerUpdate.setPassword(DigestUtil.encrypt(updatePwd.getPassword()));
		sellerService.updateById(sellerUpdate);
		return R.status(true);
	}

}
